﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;


namespace ShowScreenProject
{



    public class ScreenHelper
    {
        private static double _screenWidth;
        /// <summary>
        /// 返回屏幕宽度
        /// </summary>
        public static double ScreenWidth
        {
            get
            {
                return _screenWidth;
            }
        }

        private static double _screenHeight;
        /// <summary>
        /// 返回屏幕高度
        /// </summary>
        public static double ScreenHeight
        {
            get
            {
                return _screenHeight;
            }
        }
        /// <summary>
        /// 横向数量
        /// </summary>
        public static int HorizontalCount { get => _horizontalCount; }

        /// <summary>
        /// 纵向数量
        /// </summary>
        public static int LongitudinalCount { get => _longitudinalCount; }

        /// <summary>
        /// 横向数量
        /// </summary>
        private static int _horizontalCount;
        /// <summary>
        /// 纵向数量
        /// </summary>
        private static int _longitudinalCount;

        /// <summary>
        /// 可现实窗体数量总数
        /// </summary>
        public static int ShowFrmCount { get => _horizontalCount * _longitudinalCount; }

        static ScreenHelper()
        {
            _screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            _screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
        }


        public static void ComputeFormCount(double height, double width)
        {
            if (height.CompareTo(0) > 0)
            {

                _longitudinalCount = (int)Math.Round(_screenHeight / height, 0);

            }

            if (width.CompareTo(0) > 0)
            {
                _horizontalCount = (int)Math.Round(_screenWidth / width, 0);

            }
        }

        /// <summary>
        /// 计算当前显示窗体位置
        /// </summary>
        /// <param name="frmIndex">显示窗体的标识</param>
        /// <param name="height">窗体高度</param>
        /// <param name="width">窗体宽度</param>
        /// <returns></returns>
        public static WindowsFormLocation ComputeFromLocation(int frmIndex, double height, double width)
        {
            WindowsFormLocation location = new WindowsFormLocation();

            if(frmIndex>=ShowFrmCount)
            {
                frmIndex = frmIndex % ShowFrmCount;
               
            }

            //判断是否初始化界面数量
            if (_horizontalCount == 0)
            {
                ComputeFormCount(height, width);
            }

            int rowIndex = 0;

            int rowNum = (int)( frmIndex / _horizontalCount);

            rowIndex += rowNum;

            int colIndex = 0;

            colIndex=((int)(frmIndex %_longitudinalCount));

            location.Left = colIndex * width; 
            location.Top = rowNum * height;

            //if (_horizontalCount == 1) { 

            //}

            return location;
        }



    }
}
